package com.zlkj.pro.modular.business.service.impl;

import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlkj.pro.core.util.ConvertUtil;
import com.zlkj.pro.modular.business.entity.AreaEntity;
import com.zlkj.pro.modular.business.entity.ProductTypeEntity;
import com.zlkj.pro.modular.business.mapper.AreaMapper;
import com.zlkj.pro.modular.business.model.dto.AreaDTO;
import com.zlkj.pro.modular.business.model.vo.AreaVO;
import com.zlkj.pro.modular.business.service.AreaService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author liyang
 * @date 2024/4/6
 * @time 14:34
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class AreaServiceImpl extends ServiceImpl<AreaMapper, AreaEntity> implements AreaService {

    @Override
    public PageResult<AreaVO> findPage(AreaDTO dto) {
        Integer pageNo = dto.getPageNo();
        Integer pageSize = dto.getPageSize();
        LambdaQueryWrapper<AreaEntity> queryWrapper = Wrappers.lambdaQuery();
        if (dto.getSharing() != null) {
            queryWrapper.eq(AreaEntity::getSharing, dto.getSharing());
        }
        if (StringUtils.isNotBlank(dto.getName())) {
            queryWrapper.like(AreaEntity::getName, dto.getName());
        }
        // 添加排序条件
        if (StringUtils.isNotBlank(dto.getSort()) && StringUtils.isNotBlank(dto.getOrder())) {
            if (dto.getSort().equalsIgnoreCase("id")) {
                queryWrapper.orderBy(true, dto.getOrder().equalsIgnoreCase("asc"), AreaEntity::getId);
            } else if (dto.getSort().equalsIgnoreCase("name")) {
                queryWrapper.orderBy(true, dto.getOrder().equalsIgnoreCase("asc"), AreaEntity::getName);
            } else if (dto.getSort().equalsIgnoreCase("createTime")) {
                queryWrapper.orderBy(true, dto.getOrder().equalsIgnoreCase("asc"), AreaEntity::getCreateTime);
            } else if (dto.getSort().equalsIgnoreCase("sharing")) {
                queryWrapper.orderBy(true, dto.getOrder().equalsIgnoreCase("asc"), AreaEntity::getSharing);
            }
        } else {
            queryWrapper.orderByAsc(AreaEntity::getId);
        }
        Page<AreaEntity> materialGroupEntityPage = super.page(new Page<>(pageNo, pageSize), queryWrapper);
        long total = materialGroupEntityPage.getTotal();
        List<AreaEntity> records = materialGroupEntityPage.getRecords();
        List<AreaVO> materialGroupAdminList = ConvertUtil.turn(records, AreaVO.class);
        return PageResultFactory.createPageResult(materialGroupAdminList, total, pageSize, pageNo);
    }

    @Override
    public List<AreaVO> findList(AreaDTO dto) {
        LambdaQueryWrapper<AreaEntity> queryWrapper = Wrappers.lambdaQuery();
        if(dto.getSharing()!=null){
            queryWrapper.eq(AreaEntity::getSharing,dto.getSharing());
        }
        queryWrapper.orderByDesc(AreaEntity::getCreateTime);
        List<AreaEntity> materialGroupEntityList = super.list(queryWrapper);
        return ConvertUtil.turn(materialGroupEntityList, AreaVO.class);
    }

    @Override
    public AreaVO findDetail(AreaDTO dto) {
        Long id = dto.getId();
        LambdaQueryWrapper<AreaEntity> materialGroupEntityLambdaQueryWrapper = Wrappers.lambdaQuery();
        materialGroupEntityLambdaQueryWrapper.eq(AreaEntity::getId, id);
        AreaEntity materialGroupEntity = super.getOne(materialGroupEntityLambdaQueryWrapper, Boolean.FALSE);
        return ConvertUtil.turn(materialGroupEntity, AreaVO.class);
    }

    @Override
    public boolean add(AreaDTO dto) {
        AreaEntity materialGroupEntity = ConvertUtil.turn(dto, AreaEntity.class);
        return super.save(materialGroupEntity);
    }

    @Override
    public boolean edit(AreaDTO dto) {
        AreaEntity materialGroupEntity = ConvertUtil.turn(dto, AreaEntity.class);
        return super.updateById(materialGroupEntity);
    }

    @Override
    public boolean delete(AreaDTO dto) {
        return super.removeById(dto);
    }

    @Override
    public boolean batchDelete(List<Long> ids) {
        return super.removeByIds(ids);
    }

}
